<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	  xmlns:th="http://www.thymeleaf.org"
	  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
	  lang="en">
	<head>
		<meta charset="utf-8">
		<title>Layui</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="/layui-v2.6.4/layui/css/layui.css" media="all">
		<script src="/js/jquery-3.5.1.min.js" charset="UTF-8"></script>
	</head>
	<body>

		<table class="layui-hide" id="test" lay-filter="test"></table>
		<script type="text/html" id="toolbarDemo">
			<div class="layui-btn-container">
				<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
				<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
				<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
				<button class="layui-btn layui-btn-sm" id="addfiction">新增</button>
			</div>
		</script>
		<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>
		<script src="/layui-v2.6.4/layui/layui.js" charset="utf-8"></script>

		<script th:inline="javascript" type="text/javascript">
			var adminAuthor = /*[[${session.admin.getAdminAuthority()}]]*/
					layui.use('table', function () {
						var table = layui.table;
						table.render({
							elem: '#test',
							url: 'getfictionslimit',
							parseData: function (res) { //res 即为原始返回的数据
								return {
									"code": res.code, //解析接口状态
									"msg": res.msg, //解析提示文本
									"count": res.count, //解析数据长度
							"data": res.data //解析数据列表
						}
					},
					page: true,
					limits: [3, 5, 10, 20],
					limit: 10,
					toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
					height: 'full-3',
					defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
						title: '提示',
						layEvent: 'LAYTABLE_TIPS',
						icon: 'layui-icon-tips'
					}],
					title: '用户数据表',
					cols: [
						[{
							type: 'checkbox',
							fixed: 'left'
						}, {
							field: 'id',
							title: 'ID',
							width: 200,
							fixed: 'left',
							unresize: true
						}, {
							field: 'ficName',
							title: '小说名',
							width: 180,
							edit: 'text'
						}, {
							field: 'ficAuthor',
							title: '作者',
							width: 150,
							edit: 'text'
						}, {
							field: 'ficHost',
							title: '热度',
							width: 80,
							edit: 'text',
							sort: true
						}, {
							field: 'ficImg',
							title: '图片名',
							width: 80,
							edit: 'text',
						}, {
							field: 'ficType',
							title: '所属分区',
							width: 100,
							edit: 'text',
						}, {
							field: 'gmtCreate',
							title: '创建时间',
							width: 120,
							sort: true
						}, {
							field: 'gmtModified',
							title: '修改时间',
							width: 120,
							sort: true
						}, {
							field: 'isDeleted',
							title: '状态',
							width: 50
						}, {
							fixed: 'right',
							title: '操作',
							toolbar: '#barDemo',
							width: 150
						}]
					]

				});

				table.on('toolbar(test)', function(obj) {
					var checkStatus = table.checkStatus(obj.config.id);
					switch (obj.event) {
						case 'getCheckData':
							var data = checkStatus.data;
							layer.alert(JSON.stringify(data));
							break;
						case 'getCheckLength':
							var data = checkStatus.data;
							layer.msg('选中了：' + data.length + ' 个');
							break;
						case 'isAll':
							layer.msg(checkStatus.isAll ? '全选' : '未全选');
							break;
							//自定义头工具栏右侧图标 - 提示
						case 'LAYTABLE_TIPS':
							layer.alert('这是工具栏右侧自定义的一个图标按钮');
							break;
					};
				});

				//监听行工具事件
				table.on('tool(test)', function(obj) {
					var data = obj.data;
					//console.log(obj)
					if (obj.event === 'del') {
						if (adminAuthor == 'root' || adminAuthor == "super") {
							layer.confirm('真的删除么', function (index) {
								var ficid = data.id;
								$.ajax({
									url: "/delfic"
									, type: 'get'
									, async: false
									, data: {
										id: ficid
									}
									, success: function (res) {
										if (res === "1") {
											obj.del();
											layer.msg("删除成功")
											layer.close(index);
										} else if (res === "0") {
											layer.msg("删除失败")
										}
									}
								});
							});
						} else {
							layer.msg("权限不足!")
						}
					} else if (obj.event === 'edit') {
						// layer.prompt({
						// 	formType: 2,
						// 	value: data.email
						// }, function(value, index) {
						// 	obj.update({
						// 		email: value
						// 	});
						// 	layer.close(index);
						// });
						layer.open({
							type: 1,
							skin: 'layui-layer-rim', //加上边框
							area: ['420px', '600px'], //宽高
							content:
									'<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">\n' +
									'    <legend>新增小说</legend><span class="success" id="msg"></span>\n' +
									'</fieldset>\n' +
									'<form class="layui-form" action="" method="get">\n' +
									'    <div class="layui-form-item">\n' +
									'        <label class="layui-form-label">小说名</label>\n' +
									'        <div class="layui-input-block">\n' +
									'            <input type="text" id="title" name="title" required lay-verify="required" placeholder="请输入小说标题"\n' +
									'                   autocomplete="off" class="layui-input">\n' +
									'        </div>\n' +
									'    </div>\n' +
									'    <div class="layui-form-item">\n' +
									'        <label class="layui-form-label">作者</label>\n' +
									'        <div class="layui-input-inline">\n' +
									'            <input type="text" id="author" name="author" placeholder="(选填)作者姓名"\n' +
									'                   autocomplete="off" class="layui-input">\n' +
									'        </div>\n' +
									'    </div>\n' +
									'    <div class="layui-form-item">\n' +
									'        <label class="layui-form-label">分区</label>\n' +
									'        <div class="layui-input-block">\n' +
									'            <select id="type" name="type" lay-verify="required">\n' +
									'                <option value=""></option>\n' +
									'                <option th:each="type,a : ${types}" th:value="${type}" th:text="${type}"></option>\n' +
									'            </select>\n' +
									'        </div>\n' +
									'    </div>\n' +
									'    <div class="layui-form-item">\n' +
									'        <label class="layui-form-label">是否展示</label>\n' +
									'        <div class="layui-input-block">\n' +
									'            <input id="isdel" type="checkbox" name="isdel" lay-skin="switch">\n' +
									'        </div>\n' +
									'    </div>\n' +
									'    <div class="layui-form-item layui-form-text">\n' +
									'        <label class="layui-form-label">文章内容</label>\n' +
									'        <div class="layui-input-block">\n' +
									'            <textarea id="data" name="data"  required lay-verify="required"  placeholder="请输入内容" class="layui-textarea"></textarea>\n' +
									'        </div>\n' +
									'    </div>\n' +
									'    <div class="layui-form-item">\n' +
									'        <div class="layui-input-block">\n' +
									'            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>\n' +
									'            <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
									'        </div>\n' +
									'    </div>\n' +
									'</form>'
						});
					}
				});
			});

			layui.use(['layer', 'table', "laypage", "jquery"], function () {
				var
						layer = layui.layer //弹层
						, table = layui.table //表格
						, laypage = layui.laypage //分页
						, $ = layui.jquery
				$('#addfiction').on("click", function () {
					layer.open({
						//调整弹框的大小
						area: ['500px', '550px'],
						shadeClose: true,//点击旁边地方自动关闭
						//动画
						anim: 2,
						//弹出层的基本类型
						type: 2,
						//刚才定义的弹窗页面
						content: '/addfictionWindow'//路由地址
					});
				});
			});
		</script>

	</body>
</html>
